//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.controller.api;

import com.google.common.collect.Maps;
import com.xw.common.Const;
import com.xw.model.VideosItem;
import com.xw.service.VideosItemService;
import com.xw.service.VideosService;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.util.JsonUtil;
import com.xw.vo.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/videos/")
@Scope("prototype")
public class ApiVideosController {

    @Resource
    private VideosService videosService;
    @Resource
    private VideosItemService videosItemService;

    /**
     * 记录课程学习次数
     * @param userId
     * @param videoId
     * @return
     */
    @RequestMapping(value = "addStudyCount",method = RequestMethod.POST)
    @ResponseBody
    public Object addStudyCount(String userId,String videoId) {
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.addStudyCount(userId,videoId);
        return map;
    }


    /**
     * 创建课程，并关联视频
     * @param userId             视频拥有者id
     * @param videoName          课程名称
     * @param teacherName        讲师名字
     * @param stIdOne            一级分类id
     * @param stIdTwo            二级分类id
     * @param videoDesc          课程介绍
     * @param videoImg           课程封面图
     * @param isFree             0.否 1.是
     * @param originalPrice      原价
     * @param currentPrice       当前价（售价）
     * @param publishType        发布类型 1.审核通过后立即发布 2.先提交本次审核
     * @param videosItemIds      关联的视频id，逗号分隔
     * @param videosInfoJson     资料对象json数组   [{"videoId": "3121356623","videoInfoName": "课程资料名称","videoInfoSort": 1,"videoInfoUrl": "课程资料链接", "createId": "上传用户id"}]
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "createVideo",method = RequestMethod.POST)
    @ResponseBody
    public Object createVideo(String userId,String videoName,String teacherName,String stIdOne,String stIdTwo,String videoDesc,String videoImg,Integer isFree,
                              Double originalPrice,Double currentPrice,Integer publishType,String videosItemIds,String videosInfoJson) {

        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoName", videoName);
        map.put("teacherName", teacherName);
        map.put("stIdOne", stIdOne);
        map.put("stIdTwo", stIdTwo);
        map.put("videoDesc", videoDesc);
        map.put("videoImg", videoImg);
        map.put("isFree", isFree);
        map.put("publishType", publishType);
        map.put("videosItemIds", videosItemIds);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.createVideo(userId,videoName,teacherName,stIdOne,stIdTwo,
                videoDesc,videoImg,isFree,originalPrice,currentPrice,publishType,videosItemIds,videosInfoJson);
        return map;
    }

    /**
     * 置顶(取消)自己发布的视频
     * @param userId
     * @param videoId   视频id
     * @param myRecommend   0.取消 1.置顶
     * @return
     */
    @RequestMapping(value = "setMyRecommendVideo",method = RequestMethod.POST)
    @ResponseBody
    public Object setMyRecommendVideo(String userId,String videoId,Integer myRecommend){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoId", videoId);
        map.put("myRecommend", myRecommend);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.setMyRecommendVideo(userId,videoId,myRecommend);
        return map;
    }

    /**
     *  课程 - 首页搜索 - 小程序
     * @param userId
     * @param sort      1.销量正序 2.销量倒序 3.新品正序 4.新品倒序 5.价格正序 6.价格倒序
     * @param stIdOne   一级分类id
     * @param stIdTwo   二级分类id
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "searchVideosMP",method = RequestMethod.POST)
    @ResponseBody
    public Object searchVideosMP(String userId,Integer sort,String stIdOne,String stIdTwo,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("sort", sort);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.searchVideosMP(userId,sort,stIdOne,stIdTwo,page,limit);
        return map;
    }

    /**
     * 查询用户已购买的讲师课程 - 小程序
     * @param userId
     * @param teacherId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "findBuyTeacherVideosByUserIdMP",method = RequestMethod.POST)
    @ResponseBody
    public Object findBuyTeacherVideosByUserIdMP(String userId,String teacherId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("teacherId", teacherId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.findBuyTeacherVideosByUserIdMP(userId,teacherId,page,limit);
        return map;
    }

    /**
     * 查询讲师全部课程 - 小程序
     * @param userId
     * @param teacherId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "findTeacherAllVideosMP",method = RequestMethod.POST)
    @ResponseBody
    public Object findTeacherAllVideosMP(String userId,String teacherId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("teacherId", teacherId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.findTeacherAllVideosMP(userId,teacherId,page,limit);
        return map;
    }

    /**
     * 查询讲师推荐的课程 - 小程序
     * @param userId
     * @param teacherId     讲师id
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "findTeacherRecommendVideosMP",method = RequestMethod.POST)
    @ResponseBody
    public Object findTeacherRecommendVideosMP(String userId,String teacherId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("teacherId", teacherId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.findTeacherRecommendVideosMP(userId,teacherId,page,limit);
        return map;
    }

    /**
     * 推荐课程(系统推荐)-更多 -小程序
     * @param searchName 搜索词
     * @param sort      1.销量正序 2.销量倒序 3.新品正序 4.新品倒序 5.价格正序 6.价格倒序
     * @param stIdOne   一级分类id
     * @param stIdTwo   二级分类id
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "findRecommendVideosMoreMP",method = RequestMethod.POST)
    @ResponseBody
    public Object findRecommendVideosMoreMP(String searchName,Integer sort,String stIdOne,String stIdTwo,Integer page,Integer limit){
        Map map = videosService.findRecommendVideosMoreMP(searchName,sort,stIdOne,stIdTwo,page,limit);
        return map;
    }

    /**
     * 添加子视频
     * @param userId
     * @param videoId
     * @param videosItemJson    视频对象json [{"videoId":"1","videoItemSize":15.2,"sort":1,"videoItemName":"duola1","videoUrl":"https://www.iqiyi.com/v_19rrk33vjo.html"}]
     * @return
     */
    @RequestMapping(value = "addVideoItem",method = RequestMethod.POST)
    @ResponseBody
    public Object addVideoItem(String userId,String videoId,String videosItemJson){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoId", videoId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.addVideoItem(userId,videoId,videosItemJson);
        return map;
    }


    /**
     * 试看视频
     * @param paramsMap
     * @return
     * @throws Exception
     */
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "tryAndSeeVideo",method = RequestMethod.POST)
    @ResponseBody
    public Object tryAndSeeVideo(@RequestBody Map<String,Object> paramsMap) throws  Exception{

        //验证参数
        String str = AppUtil.checkIsNull(paramsMap);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

        VideosVo videosVo = new VideosVo();
        Map<String ,Object> videosMap = (Map<String, Object>)paramsMap.get("videosVo");
        JsonUtil.mapToObject(videosMap,videosVo);

        paramsMap.put("videosVo",videosVo);
        return videosService.getVideoTeacher(paramsMap);
    }

    /**
     * 发布视频
     * @param userId
     * @param videosId
     * @return
     */
    @RequestMapping(value = "publish",method = RequestMethod.POST)
    @ResponseBody
    public Object publish(String userId,String videosId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videosId", videosId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.publish(userId,videosId);
        return map;
    }

    /**
     *  修改子视频名称和地址
     * @param userId
     * @param flag          1.修改名称 2.修改视频
     * @param videoItemId
     * @param videoItemName
     * @param videoUrl
     * @param videoItemSize
     * @param videoId
     * @return
     */
    @RequestMapping(value = "updateNameOrUrl",method = RequestMethod.POST)
    @ResponseBody
    public Object updateNameOrUrl(String userId,Integer flag,String videoItemId,String videoItemName,String videoUrl,Double videoItemSize,String videoId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoItemId", videoItemId);
        map.put("flag", flag);
        map.put("videoId", videoId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = videosService.updateNameOrUrl(userId,flag,videoItemId,videoItemName,videoUrl,videoItemSize,videoId);
        return map;
    }

    /**
     * 修改排序
     * @param userId
     * @param videoItemId
     * @param sort
     * @return
     */
    @RequestMapping(value = "updateSort",method = RequestMethod.POST)
    @ResponseBody
    public Object updateSort(String userId,String videoItemId,Integer sort){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoItemId", videoItemId);
        map.put("sort", sort);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.updateSort(userId,videoItemId,sort);
        return map;
    }


    /**
     * 删除课程
     * @param userId
     * @param videosId
     * @return
     */
    @RequestMapping(value = "delByVideoId",method = RequestMethod.POST)
    @ResponseBody
    public Object delByVideoId(String userId,String videosId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videosId", videosId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = videosService.delByVideoId(userId,videosId);
        return map;
    }

    /**
     * 删除子视频
     * @param userId
     * @param videoItemId
     * @return
     */
    @RequestMapping(value = "delByVideosItemId",method = RequestMethod.POST)
    @ResponseBody
    public Object delByVideosItemId(String userId,String videoItemId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoItemId", videoItemId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = videosService.delByVideosItemId(userId,videoItemId);
        return map;
    }

    /**
     * 根据一个子视频查询所有关于视频的详情信息
     * @param userId
     * @param videoItemId
     * @return
     */
    @RequestMapping(value = "findVideosDetailByVideoItemId",method = RequestMethod.POST)
    @ResponseBody
    public Object findVideosDetailByVideoItemId(String userId,String videoItemId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoItemId", videoItemId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = videosService.findVideosDetailByVideoItemId(userId,videoItemId);
        return map;
    }

    /**
     * 根据视频id查询视频详情 (正常和下架的视频都会查询，视频如果下架或删除购买之后的用户还可以正常观看)
     * @param userId
     * @param videoId
     * @return
     */
    @RequestMapping(value = "findVideosByVideoId",method = RequestMethod.POST)
    @ResponseBody
    public Object findVideosByVideoId(String userId,String videoId){

        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoId", videoId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = videosService.findVideosByVideoId(userId,videoId);
        return map;
    }


    /**
     * 课程中心
     * @param searchName 搜索词
     * @param sort      1.销量正序 2.销量倒序 3.新品正序 4.新品倒序 5.价格正序 6.价格倒序
     * @param stIdOne   一级分类id
     * @param stIdTwo   二级分类id
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "findVideosByType",method = RequestMethod.POST)
    @ResponseBody
    public Object findVideosByType(String searchName,Integer sort,String stIdOne,String stIdTwo,Integer page,Integer limit){
        Map map = videosService.findVideosByType(searchName,sort,stIdOne,stIdTwo,page,limit);
        return map;
    }


    /**
     * 搜索视频
     * @param searchName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "searchVideos",method = RequestMethod.POST)
    @ResponseBody
    public Object searchVideos(String searchName,Integer page,Integer limit){
        Map map = videosService.searchVideos(searchName,page,limit);
        return map;
    }


    /**
     * 修改单个视频
     * @param userId
     * @param videoItemId   主键id
     * @param videoItemName 视频名称
     * @param videoItemSize 时长
     * @param videoUrl  视频地址
     * @param sort      排序
     * @param videoId   关联（播单） 视频id
     * @return
     */
    @RequestMapping(value = "updateVideoItem",method = RequestMethod.POST)
    @ResponseBody
    public Object updateVideoItem(String userId,String videoItemId,String videoItemName,Double videoItemSize,String videoUrl,Integer sort,String videoId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoItemId", videoItemId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.updateVideoItem(userId,videoItemId,videoItemName,videoItemSize,videoUrl,sort,videoId);
        return map;
    }

     /**
      * 视频上传
      * @param userId            视频上传者id
      * @param videoName         课程名称
      * @param teacherName       讲师名字
      * @param stIdOne           一级分类id
      * @param stIdTwo           二级分类id
      * @param videoDesc         课程介绍
      * @param videoImg          课程封面图
      * @param isFree            0.否 1.是
      * @param originalPrice     原价
      * @param currentPrice      当前价（售价）
      * @param publishType       发布类型 1.审核通过后立即发布 2.先提交本次审核 null
      * @param videosItemJson    视频对象json [{"videoItemSize":15.2,"sort":1,"videoItemName":"duola1","videoUrl":"https://www.iqiyi.com/v_19rrk33vjo.html"}]
      * @return
      */
    @RequestMapping(value = "saveVideos",method = RequestMethod.POST)
    @ResponseBody
    public Object saveVideos(String userId,String videoName,String teacherName,String stIdOne,String stIdTwo,String videoDesc,
                             String videoImg,Integer isFree,Double originalPrice,Double currentPrice,Integer publishType,String videosItemJson){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoName", videoName);
        map.put("teacherName", teacherName);
        map.put("stIdOne", stIdOne);
        map.put("stIdTwo", stIdTwo);
        map.put("videoDesc", videoDesc);
        map.put("videoImg", videoImg);
        map.put("publishType", publishType);
        map.put("videosItemJson", videosItemJson);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = videosService.saveVideos(userId,videoName,teacherName,stIdOne,stIdTwo,videoDesc,videoImg,isFree,originalPrice,currentPrice,publishType,videosItemJson);
        return map;
    }


    /**
     * 修改视频
     * @param videoId           视频id
     * @param userId            视频上传者id
     * @param videoName         课程名称
     * @param teacherName       讲师名字
     * @param stIdOne           一级分类id
     * @param stIdTwo           二级分类id
     * @param videoDesc         课程介绍
     * @param videoImg          课程封面图
     * @param isFree            0.否 1.是
     * @param originalPrice     原价
     * @param currentPrice      当前价（售价）
     * @param publishType       发布类型 1.审核通过后立即发布 2.先提交本次审核 null
     * @return
     */
    @RequestMapping(value = "updateVideo",method = RequestMethod.POST)
    @ResponseBody
    public Object updateVideo(String userId,String videoId,String videoName,String teacherName,String stIdOne,String stIdTwo,String videoDesc,
                             String videoImg,Integer isFree,Double originalPrice,Double currentPrice,Integer publishType){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("videoId", videoId);
        map.put("videoName", videoName);
        map.put("teacherName", teacherName);
        map.put("stIdOne", stIdOne);
        map.put("stIdTwo", stIdTwo);
        map.put("videoDesc", videoDesc);
        map.put("videoImg", videoImg);
        map.put("publishType", publishType);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();

        map = videosService.updateVideo(userId,videoId,videoName,teacherName,stIdOne,stIdTwo,videoDesc,videoImg,isFree,originalPrice,currentPrice,publishType);
        return map;
    }



    /**
     * @需求描述:查询课程集
     * @创建人:whh
     * @创建时间: 2019/11/11
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @PostMapping(value = "getTeacherVideo")
    @ResponseBody
    public Object getTeacherVideoAction(@RequestBody Map<String,Object> paramsMap) throws Exception {

        //验证参数
        String str = AppUtil.checkIsNull(paramsMap);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

        UserInfoVo userInfoVo = new UserInfoVo();
        Map<String ,Object> userMap = (Map<String, Object>)paramsMap.get("userInfoVo");
        if (null != userMap){
            JsonUtil.mapToObject(userMap,userInfoVo);
        }

        paramsMap.put("userInfoVo",userInfoVo);
        return videosService.getTeacherVideo(paramsMap);
    }


    /**
     * @需求描述:查询课程讲师简介
     * @创建人:whh
     * @创建时间: 2019/11/13
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @PostMapping(value = "getVideoTeacher")
    @ResponseBody
    public Object getVideoTeacherAction(@RequestBody Map<String,Object> paramsMap) throws Exception {

        //验证参数
        String str = AppUtil.checkIsNull(paramsMap);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

        VideosVo videosVo = new VideosVo();
        Map<String ,Object> videosMap = (Map<String, Object>)paramsMap.get("videosVo");
        JsonUtil.mapToObject(videosMap,videosVo);

        paramsMap.put("videosVo",videosVo);
        return videosService.getVideoTeacher(paramsMap);
    }


    /**
     * @需求描述:测试接口
     * @创建人:whh
     * @创建时间: 2019/11/11
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @PostMapping(value = "test")
    @ResponseBody
    public Object test(@RequestBody Map<String,Object> map) throws Exception {

        List<Map<String,Object>> videosItems = ( List<Map<String,Object>>) map.get("videoItems");
        List<VideosItemVo> videosItemVos = new ArrayList<>();
       for (Map<String,Object> videoItem : videosItems){
           VideosItemVo videosItemVo = new VideosItemVo();
           JsonUtil.mapToObject(videoItem,videosItemVo);
           videosItemVos.add(videosItemVo);
       }

        return videosItemVos;


    }


    @RequestMapping(value = "uploadVideos",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: uploadVideos
     * @Description: TODO
     * @Author: moke
     * @createTime:2019/11/18 15:48
     * @param userId             视频拥有者id
     * @param createId           视频上传者id
     * @param isCreateVideos     1 创建课程 2 添加已有课程 3 暂不创建课程
     * @param videoName          课程名称
     * @param teacherName        讲师名字
     * @param stIdOne            一级分类id
     * @param stIdTwo            二级分类id
     * @param videoDesc          课程介绍
     * @param videoImg           课程封面图
     * @param isFree             0.否 1.是
     * @param originalPrice      原价
     * @param currentPrice       当前价（售价）
     * @param publishType        发布类型 1.审核通过后立即发布 2.先提交本次审核
     * @param videosItemList     新上传的视频对象json
     * @param isAddVideosInfo     1 添加 2 不添加
     * @param videosInfoJson     资料对象json
     * @param alreadyVideosItemList   修改的课程视频json
     * @Return: java.lang.Object
     */
    public Object uploadVideos(@RequestBody Map<String,Object> paramsMap, HttpServletRequest request,String alreadyVideosItemList)throws  Exception{

        System.err.println(paramsMap.get("isCreateVideos").toString());

        String userId = null;
        String stIdTwo = null;
        String createId = null;
        String videoName= null;
        String teacherName= null;
        String stIdOne = null;
        String videosId = null;
        String videoDesc= null;
        String videoImg = null;
        Double originalPrice = null;
        Double currentPrice = null;
        Integer publishType = null;
        Integer isAddVideosInfo = null;
        Integer isCreateVideos = Integer.parseInt(paramsMap.get("isCreateVideos").toString());
        Integer isFree = null;
        List<VideosItem> videosItemList = new ArrayList<>();
        List<VideosInfoVo> videosInfoList = new ArrayList<>();
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", paramsMap.get("userId"));
        userId = (String)paramsMap.get("userId");
        map.put("videosItemList", paramsMap.get("videosItemList"));
        if (paramsMap.get("isCreateVideos").toString().equals("1")){
            map.put("createId", paramsMap.get("createId"));
            map.put("videoName", paramsMap.get("videoName"));
            map.put("stIdOne", paramsMap.get("stIdOne"));
            map.put("stIdTwo", paramsMap.get("stIdTwo"));
            map.put("videoDesc", paramsMap.get("videoDesc"));
            map.put("videoImg", paramsMap.get("videoImg"));
            map.put("publishType", paramsMap.get("publishType"));
            map.put("teacherName", paramsMap.get("teacherName"));
            map.put("isFree", paramsMap.get("isFree"));
            String str = AppUtil.checkIsNull(map);
            if (!str.equals(Const.SUCCESS_CODE_STR)) {
                return JsonData.fail(Const.parameterError(str));
            }
            createId = paramsMap.get("createId").toString();
            videoName = paramsMap.get("videoName").toString();
            teacherName = paramsMap.get("teacherName").toString();
            stIdOne = paramsMap.get("stIdOne").toString();
            stIdTwo = paramsMap.get("stIdTwo").toString();
            videoDesc = paramsMap.get("videoDesc").toString();
            videoImg = paramsMap.get("videoImg").toString();
            isFree = Integer.parseInt(paramsMap.get("isFree").toString());

            if ("" != paramsMap.get("originalPrice") && null != paramsMap.get("originalPrice")){
                originalPrice = Double.parseDouble(paramsMap.get("originalPrice").toString());
            }
            if ("" != paramsMap.get("currentPrice") && null != paramsMap.get("currentPrice")){
                currentPrice = Double.parseDouble(paramsMap.get("currentPrice").toString());
            }
            publishType = Integer.parseInt(paramsMap.get("publishType").toString());
            if (paramsMap.get("isAddVideosInfo").toString().equals("1")){
                isAddVideosInfo = Integer.parseInt(paramsMap.get("isAddVideosInfo").toString());
                map.put("videosInfoList", paramsMap.get("videosInfoList"));
                    List<Map<String,Object>> videoInfoMap = (List<Map<String,Object>>)paramsMap.get("videosInfoList");
                    if (!str.equals(Const.SUCCESS_CODE_STR)) {
                        return JsonData.fail(Const.parameterError(str));
                    }
                    for (Map map1 : videoInfoMap){
                        VideosInfoVo videosInfoVo = new VideosInfoVo();
                        JsonUtil.mapToObject(map1,videosInfoVo);
                        videosInfoList.add(videosInfoVo);
                    }
            }
        }
        if (paramsMap.get("isCreateVideos").toString().equals("2")){
            map.put("videosId", paramsMap.get("videosId"));
            String str = AppUtil.checkIsNull(map);
            if (!str.equals(Const.SUCCESS_CODE_STR)) {
                return JsonData.fail(Const.parameterError(str));
            }
            videosId = paramsMap.get("videosId").toString();
            alreadyVideosItemList = paramsMap.get("alreadyVideosItemList").toString();
        }
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }

       List<Map<String,Object>> videoItemMap = (List<Map<String,Object>>)paramsMap.get("videosItemList");
       for (Map map1 : videoItemMap){
           VideosItem videosItem = new VideosItem();
           JsonUtil.mapToObject(map1,videosItem);
           videosItemList.add(videosItem);
       }
        map.clear();
        map = videosService.uploadVideos(userId,createId,isCreateVideos,videosId,videoName,teacherName,stIdOne,stIdTwo,
                videoDesc,videoImg,isFree,originalPrice,currentPrice,publishType,videosItemList,videosInfoList,alreadyVideosItemList);
        return map;
    }


    @RequestMapping(value = "getCheckingVideoItems",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getCheckingVideoItems
     * @Description: 获取审核中的视频
     * @Author: moke
     * @createTime: 2019/11/18 18:24
     * @param userId 拥有者id
     * @param page
     * @param limit
     * @Return: java.lang.Object
     */
    public Object getCheckingVideoItems(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Integer videoStatus = 0;
        Map resultMap = videosService.getVideoItems(userId,videoStatus,page,limit);
        return resultMap;
    }

    /**
     * 查询审核通过没有关联课程的视频
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "getPassVideoItemNoVideos",method = RequestMethod.POST)
    @ResponseBody
    public Object getPassVideoItemNoVideos(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosService.getPassVideoItemNoVideos(userId,page,limit);
        return resultMap;
    }

    @RequestMapping(value = "getCheckPassVideoItems",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getCheckPassVideoItems
     * @Description: 获取审核通过的视频
     * @Author: moke
     * @createTime: 2019/11/18 18:24
     * @param userId  拥有者id
     * @param page
     * @param limit
     * @Return: java.lang.Object
     */
    public Object getCheckPassVideoItems(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Integer videoStatus = 1;
        Map resultMap = videosService.getVideoItems(userId,videoStatus,page,limit);
        return resultMap;
    }

    /**
     * 添加已有课程（获取审核通过和待审核的课程）
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "getCheckingAndPassVideo",method = RequestMethod.POST)
    @ResponseBody
    public Object getCheckingAndPassVideo(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosService.getCheckingAndPassVideo(userId,page,limit);
        return resultMap;
    }

    @RequestMapping(value = "getCheckNoPassVideoItems",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getCheckNoPassVideoItems
     * @Description: 获取审核不通过的视频
     * @Author: moke
     * @createTime: 2019/11/19 18:24
     * @param userId  拥有者id
     * @param page
     * @param limit
     * @Return: java.lang.Object
     */
    public Object getCheckNoPassVideoItems(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Integer videoStatus = 2;
        Map resultMap = videosService.getVideoItems(userId,videoStatus,page,limit);
        return resultMap;
    }

    @RequestMapping(value = "getCheckingVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getCheckingVideo
     * @Description: 获取审核中的课程
     * @Author: moke
     * @createTime: 2019/11/18 18:23
     * @param userId 拥有者id
     * @param page
     * @param limit
     * @Return: java.lang.Object
     */
    public Object getCheckingVideo(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosService.getCheckingVideo(userId, page,limit);
        return resultMap;
    }


    @RequestMapping(value = "getCheckPassVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getCheckingVideo
     * @Description: 获取审核通过和审核中的课程
     * @Author: moke
     * @createTime: 2019/11/19 17:23
     * @param userId 拥有者id
     * @param page
     * @param limit
     * @Return: java.lang.Object
     */
    public Object getCheckPassVideo(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosService.getCheckPassVideo(userId,page,limit);
        return resultMap;
    }

    @RequestMapping(value = "getCheckNoPassVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getCheckNoPassVideo
     * @Description: 获取审核不通过的课程
     * @Author: moke
     * @createTime: 2019/11/19 17:23
     * @param userId 拥有者id
     * @param page
     * @param limit
     * @Return: java.lang.Object
     */
    public Object getCheckNoPassVideo(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosService.getCheckNoPassVideo(userId, page,limit);
        return resultMap;
    }


    /**
     * 添加已有课程
     * @param userId       用户id
     * @param videoId      课程id
     * @param videoItemIdJson  添加的子视频 [{"videoItemId":"q","sort":1}]
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "addUsedVideo",method = RequestMethod.POST)
    @ResponseBody
    public Object addUsedVideo(String userId,String videoId,String videoItemIdJson){
        Map map = Maps.newHashMap();
        map.put("userId",userId);
        map.put("videoId",videoId);
        map.put("videoItemIdJson",videoItemIdJson);
        //判断传参
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.addUsedVideo(userId, videoId, videoItemIdJson);
        return map;
    }


    @RequestMapping(value = "setTopVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: setTopVideoItem
     * @Description: 将某个课程置顶
     * @Author: moke
     * @createTime: 2019/11/19 16:58
     * @param videoId
     * @param videoItemId
     * @Return: java.lang.Object
     */
    public Object setTopVideo(String videoId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videoId", videoId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Integer isTop = 1;
        Map resultMap = videosItemService.setTopVideo(videoId,isTop);
        return resultMap;
    }

    @RequestMapping(value = "cancelTopVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: cancelTopVideoItem
     * @Description: 将某个课程取消置顶
     * @Author: moke
     * @createTime: 2019/11/19 16:58
     * @param videoId
     * @param videoItemId
     * @Return: java.lang.Object
     */
    public Object cancelTopVideo(String videoId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videoId", videoId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Integer isTop = 0;
        Map resultMap = videosItemService.setTopVideo(videoId,isTop);
        return resultMap;
    }

    @RequestMapping(value = "setTopVideoItem",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: setTopVideoItem
     * @Description: 将某个视频置顶
     * @Author: moke
     * @createTime: 2019/11/19 16:58
     * @param videoId
     * @param videoItemId
     * @Return: java.lang.Object
     */
    public Object setTopVideoItem(String videoItemId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videoItemId", videoItemId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Integer isTop = 1;
        Map resultMap = videosItemService.setTopVideoItem(videoItemId,isTop);
        return resultMap;
    }

    @RequestMapping(value = "cancelTopVideoItem",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: cancelTopVideoItem
     * @Description: 将某个视频取消置顶
     * @Author: moke
     * @createTime: 2019/11/19 16:58
     * @param videoId
     * @param videoItemId
     * @Return: java.lang.Object
     */
    public Object cancelTopVideoItem(String videoItemId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videoItemId", videoItemId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Integer isTop = 0;
        Map resultMap = videosItemService.setTopVideoItem(videoItemId,isTop);
        return resultMap;
    }

    @RequestMapping(value = "issueVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: issueVideo
     * @Description: 发布课程
     * @Author: moke
     * @createTime: 2019/11/19 16:58
     * @param videoId  课程id
     * @param videoItemId 视频id
     * @Return: java.lang.Object
     */
    public Object issueVideo(String videoId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videoId", videoId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosService.issueVideo(videoId);
        return resultMap;
    }

    @RequestMapping(value = "reeditVideoItem",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: reeditVideoItem
     * @Description: 重新编辑视频
     * @Author: moke
     * @createTime: 2019/11/20 14:58
     * @param videoId
     * @param videoUrl
     * @param videoName
     * @Return: java.lang.Object
     */
    public Object reeditVideoItem(String videoItemId,String videoUrl, String videoItemName, Integer sort, Double videoItemSize){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videoItemId", videoItemId);
        map.put("videoUrl", videoUrl);
        map.put("videoItemName", videoItemName);
        map.put("sort", sort);
        map.put("videoItemSize", videoItemSize);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosItemService.reeditVideoItem(videoItemId,videoUrl,videoItemName,sort,videoItemSize);
        return resultMap;
    }

    @RequestMapping(value = "reeditVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: reeditVideo
     * @Description: 重新编辑课程
     * @Author: moke
     * @createTime: 2019/11/20 17:19
     * @param videosId          课程id
     * @param videoName         课程名字
     * @param stIdOne           一级分类
     * @param stIdTwo           二级分类
     * @param videoDesc         课程简介
     * @param videoImg          课程封面
     * @param isFree            是否免费
     * @param originalPrice     原价
     * @param currentPrice      当前价
     * @param publishType       发布类型 1.审核通过后立即发布 2.先提交本次审核
     * @param videosItemJson    视频json
     * @param videosInfoJson    资料json
     * @Return: java.lang.Object
     */
    public Object reeditVideo(@RequestBody Map<String,Object> paramsMap) throws Exception {
        String videosId = (String)paramsMap.get("videosId");
        String videoName = (String)paramsMap.get("videoName");
        String stIdOne = (String)paramsMap.get("stIdOne");
        String stIdTwo = (String)paramsMap.get("stIdTwo");
        String videoDesc = (String)paramsMap.get("videoDesc");
        String videoImg = (String)paramsMap.get("videoImg");
        Integer isFree = Integer.valueOf(String.valueOf(paramsMap.get("isFree")));
        Double originalPrice = Double.valueOf(String.valueOf(paramsMap.get("originalPrice")));
        Double currentPrice = Double.valueOf(String.valueOf(paramsMap.get("currentPrice")));
        Integer publishType = Integer.valueOf(String.valueOf(paramsMap.get("publishType")));
        List<Map<String,Object>> videoItemMap = (List<Map<String,Object>>)paramsMap.get("videosItemList");
        List<Map<String,Object>> videoInfoMap = (List<Map<String,Object>>)paramsMap.get("videosInfoList");
        List<VideosItem> videosItemList = new ArrayList<>();
        List<VideosInfoVo> videosInfoList = new ArrayList<>();
        /*视频格式转换*/
        if(videoItemMap.size()>0){
            for (Map map1 : videoItemMap){
                VideosItem videosItem = new VideosItem();
                JsonUtil.mapToObject(map1,videosItem);
                videosItemList.add(videosItem);
            }
        }else{
            return JsonData.fail(Const.parameterError("videosItemList"));
        }
        if(videoInfoMap.size()>0){
            /*课程资料格式转换*/
            for (Map map1 : videoInfoMap){
                VideosInfoVo videosInfoVo = new VideosInfoVo();
                JsonUtil.mapToObject(map1,videosInfoVo);
                videosInfoList.add(videosInfoVo);
            }
        }
        Map resultMap = videosService.reeditVideo(videosId,videoName,stIdOne,stIdTwo,videoDesc,videoImg,isFree,originalPrice,currentPrice,
                publishType,videosItemList,videosInfoList);
        return resultMap;
    }

    @RequestMapping(value = "getAllVideos",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getAllVideos
     * @Description: 查看全部课程
     * @Author: moke
     * @createTime: 2019/11/20 15:56
     * @param userId
     * @Return: java.lang.Object
     */
    public Object getAllVideos(String userId,Integer page,Integer limit){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.getAllVideos(userId,page,limit);
        return map;
    }

    @RequestMapping(value = "getVideosMessage",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: getVideosMessage
     * @Description: 查看课程信息
     * @Author: moke
     * @createTime: 2019/11/20 15:58
     * @param userId
     * @Return: java.lang.Object
     */
    public Object getVideosMessage(String videoId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videoId", videoId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        Map resultMap = videosService.getVideosMessage(videoId);
        return resultMap;
    }

    @RequestMapping(value = "restartSortVideoItems",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: restartSortVideoItems
     * @Description: 查看课程信息
     * @Author: moke
     * @createTime: 2019/11/20 15:58
     * @param userId
     * @Return: java.lang.Object
     */
    public Object restartSortVideoItems(@RequestBody List<VideosItem> videosItemList){
        //判断传参
        if(CollectionUtils.isEmpty(videosItemList)){
            return JsonData.fail(Const.parameterError("videosItemList"));
        }
        Map resultMap = videosItemService.restartSortVideoItems(videosItemList);
        return resultMap;
    }

    //todo 下架视频
    @RequestMapping(value = "recordNumbersOfVideo",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: recordNumbersOfVideo
     * @Description: 记录学习人数
     * @author: moke
     * @createTime: 2019/12/16 15:51
     * @param videosId
     * @Return: java.lang.Object
     */
    public Object recordNumbersOfVideo(String videosId,String videoItemId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videosId", videosId);
        map.put("videoItemId", videoItemId);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.recordNumbersOfVideo(videosId,videoItemId);
        return map;
    }

    @RequestMapping(value = "soldOutVideos",method = RequestMethod.POST)
    @ResponseBody
    /**
     * @MethodName: soldOutVideos
     * @Description: 下架视频
     * @author: moke
     * @createTime: 2019/12/16 15:51
     * @param videosId
     * @Return: java.lang.Object
     */
    public Object soldOutVideos(String videosId){
        //判断传参
        Map map = Maps.newHashMap();
        map.put("videosId", videosId);

        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = videosService.soldOutVideos(videosId);
        return map;
    }


}
